package org.me.SendSMS;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.me.SendSMS.adapter.ContactAdapter;
import org.me.SendSMS.data.ContactDetails;
import org.me.SendSMS.utli.DateUtli;
import org.me.SendSMS.utli.GlobalVariable;
import org.me.SendSMS.utli.ReadContact;

import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;
import com.google.android.apps.analytics.GoogleAnalyticsTracker;





import android.app.Activity;
import android.app.ActivityManager;
import android.app.AlarmManager;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.app.ActivityManager.RunningServiceInfo;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;


public class SendSMSActivity extends Activity implements OnClickListener,OnLongClickListener {

	Bundle extra;
	Bundle extra1;
	Button buttonSend,buttonStop;
	EditText edittextPhoneNo;
	EditText textSMS;
	Context mContext;
	
	CheckBox cbPickTime,cbCheckEveryday;
	LinearLayout linearsetTime,linearsetDate;
	
	TextView txtTime,txtDate,titleSMS;
	TextView txtTimeLoop,txtDateLoop;
	
	ImageView imgUp,imgDown;
	ImageView imgEmail;
	
	ScrollView mScrollView;
	
	LinearLayout linearTimeLoop,linearRepateDate;
	
	LinearLayout linearHiddenPickTime, linearHiddenRepeate, linearButtonSendSMS,linearButtonPickTime,llAdMode2;
	
	Button btnSendNow, btnCancel;
	
	ListView listviewPhone;
	
	ContactAdapter adapter;
	String text = "";
	private static final long THIRTY_SECONDS_MILLIS = 45 * 1000;
	private GoogleAnalyticsTracker tracker;
	List<ContactDetails>list = new ArrayList<ContactDetails>();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		tracker = GoogleAnalyticsTracker.getInstance();
	     // Start the tracker in manual dispatch mode...
	    tracker.startNewSession("UA-35081259-1", this);
		setContentView(R.layout.main);
		mContext = this;
		GlobalVariable.CONTEXT = this;
		mAlarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
		
		linearHiddenPickTime= (LinearLayout) findViewById(R.id.linearHiddenPickTime);
		linearHiddenRepeate	= (LinearLayout) findViewById(R.id.linearHiddenRepeatTime);
		linearButtonSendSMS = (LinearLayout) findViewById(R.id.linearButtonSendSMSNow);
		linearButtonPickTime= (LinearLayout) findViewById(R.id.linearButtonPickTime);
		llAdMode2			= (LinearLayout) findViewById(R.id.llAdmob2);
		
		btnSendNow			= (Button) findViewById(R.id.buttonSendNow);
		btnCancel			= (Button) findViewById(R.id.btnCancelNow);
		
		
		titleSMS			= (TextView) findViewById(R.id.textViewSMS);
		mScrollView			= (ScrollView) findViewById(R.id.ScrollView);
		buttonSend 			= (Button) findViewById(R.id.buttonStart);
		buttonStop			= (Button) findViewById(R.id.btnStop);
		
		edittextPhoneNo 	= (EditText) findViewById(R.id.editTextPhoneNo);
		textSMS 			= (EditText) findViewById(R.id.editTextSMS);
		
		cbPickTime 			= (CheckBox) findViewById(R.id.cbPickTime);
		cbCheckEveryday 	= (CheckBox) findViewById(R.id.cbcheckEveryday);
		
		linearsetTime		= (LinearLayout) findViewById(R.id.linearSetTime);
		linearsetDate		= (LinearLayout)findViewById(R.id.linearSetDate);
		
		txtTime 			= (TextView) findViewById(R.id.txtTime);
		txtDate 			= (TextView) findViewById(R.id.txtDate);
		
		txtTimeLoop 		= (TextView) findViewById(R.id.txtTimeLoop);
		txtDateLoop			= (TextView) findViewById(R.id.txtDateLoop);
		
		imgUp				= (ImageView) findViewById(R.id.imgUp);
		imgDown				= (ImageView) findViewById(R.id.imgDown);
		imgEmail			= (ImageView) findViewById(R.id.email);
		
		linearTimeLoop		= (LinearLayout) findViewById(R.id.linearTimeLoop);
		linearRepateDate	= (LinearLayout) findViewById(R.id.linearRepateDate);
		
		listviewPhone 		= (ListView)findViewById(R.id.listPhone);
		 mAlarmSender = PendingIntent.getService(  // set up an intent for a call to a service
		            SendSMSActivity.this,  // the current context
		            0,  // request code (not used)
		            new Intent(SendSMSActivity.this, AlarmService.class),  // A new Service intent
		            0   // flags (none are required for a service)
		        );
		
		llAdMode2 = (LinearLayout)findViewById(R.id.llAdmob2);
		

		try {
			AdView adView = new AdView(this, AdSize.BANNER, "a1503c30e415e29");
			  
		    // Add the adView to it
			llAdMode2.addView(adView);
		     
		    // Initiate a generic request to load it with an ad
		    AdRequest request = new AdRequest();
		    request.setTesting(true);

		    adView.loadAd(request);  
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		
		
		GlobalVariable.CHECK_START = true;
		
		if(GlobalVariable.RANDOM)
		{
			edittextPhoneNo.setText(GlobalVariable.numberHidden);
			Toast.makeText(getApplicationContext(),
					"Message is save !",
					Toast.LENGTH_LONG).show();	
		}
		
		
		//Lấy danh sách số điện thoại
		list 				= ReadContact.ReadContactDetail(mContext); 
		
		
		adapter 			= new ContactAdapter(mContext, GlobalVariable.listContact);
		listviewPhone.setAdapter(adapter);
		if(imgDown.getVisibility() == View.VISIBLE)
		{
			listviewPhone.setVisibility(View.VISIBLE);
		}
		else
		{
			listviewPhone.setVisibility(View.GONE);
		}
		
		String num 				= GlobalVariable.getValueKeyString(mContext, GlobalVariable.number);
		if(num!=null)
		{
			edittextPhoneNo.setText(num);
		}
		
		String msg 				= GlobalVariable.getValueKeyString(mContext, GlobalVariable.message);
		if(msg!=null)
		{
			textSMS.setText(msg);
		}
	
	
		
		String pickTimeTrue 	= GlobalVariable.getValueKeyString(mContext, GlobalVariable.picktime);
		String checkeverydate 	= GlobalVariable.getValueKeyString(mContext, GlobalVariable.checkeverydate);
		if(pickTimeTrue==null)
		{
			cbPickTime.setChecked(false);
			GlobalVariable.SetDefaultData(mContext, GlobalVariable.picktime, "false");
		}
		
		if(checkeverydate==null)
		{
			cbCheckEveryday.setChecked(false);
			GlobalVariable.SetDefaultData(mContext, GlobalVariable.checkeverydate, "false");
		}
		
		if(pickTimeTrue!=null)
		{
			if(pickTimeTrue.equals("true"))
			{
				cbPickTime.setChecked(true);
			}
			else
			{
				cbPickTime.setChecked(false);
			}
		}
		if(checkeverydate!=null)
		{
			if(checkeverydate.equals("true"))
			{
				cbCheckEveryday.setChecked(true);
			}
			else
			{
				cbCheckEveryday.setChecked(false);
			}
		}
		
		
		Log.w("create", "DateUtli.SpilitTime() = "+DateUtli.SpilitDate());
		
		Log.w("create", "DateUtli.SpilitTime() = "+DateUtli.SpilitTime());
		String time = GlobalVariable.getValueKeyString(mContext, GlobalVariable.time);
		String timeloop = GlobalVariable.getValueKeyString(mContext, GlobalVariable.timeloop);
		String date = GlobalVariable.getValueKeyString(mContext, GlobalVariable.date);
		String date_repeat = GlobalVariable.getValueKeyString(mContext, GlobalVariable.dateRepeat); 
		
		if(!GlobalVariable.START_DATE_REPEAT && date_repeat==null)
		{
			txtDateLoop.setText(GlobalVariable.ARRAY_ID);
			GlobalVariable.SetDefaultData(mContext, GlobalVariable.dateRepeat, GlobalVariable.ARRAY_ID);
			
		}
		if(!GlobalVariable.STARTTIME && time==null)
		{
			txtTime.setText(DateUtli.SpilitTime());
			GlobalVariable.SetDefaultData(mContext, GlobalVariable.time, DateUtli.SpilitTime());
		}
		
		if(!GlobalVariable.STARTDATE && date==null)
		{
			txtDate.setText(DateUtli.SpilitDate());
			GlobalVariable.SetDefaultData(mContext, GlobalVariable.date, DateUtli.SpilitDate());
		}
		if(!GlobalVariable.STARTTIMELOOP&&timeloop==null)
		{
			txtTimeLoop.setText(DateUtli.SpilitTime());
			GlobalVariable.SetDefaultData(mContext, GlobalVariable.timeloop, DateUtli.SpilitTime());
		}
		
		if(date_repeat!=null)
		{
			txtDateLoop.setText(date_repeat);
			
		}
		
		if(time!=null)
		{
			txtTime.setText(time);
			Log.e("hung", "vo day dung khong = "+time);
			GlobalVariable.HOURS_CURRENT =Integer.parseInt(time.split(":")[0]);
			GlobalVariable.MINUTE_CURRENT =Integer.parseInt(time.split(":")[1]);
		}
		else
		{
			txtTime.setText(DateUtli.SpilitTime());
		}
		
		if(timeloop!=null)
		{
			txtTimeLoop.setText(timeloop);
			GlobalVariable.HOURS_CURRENT_LOOP =Integer.parseInt(timeloop.split(":")[0]);
			GlobalVariable.MINUTE_CURRENT_LOOP =Integer.parseInt(timeloop.split(":")[1]);
		}
		else
		{
			txtTime.setText(DateUtli.SpilitTime());
		}
		
		if(date!=null)
		{
			txtDate.setText(date);
			Log.e("hung", "vo day dung khong = "+date);
			GlobalVariable.DATE_CURRENT =Integer.parseInt(date.split("/")[0]);
			GlobalVariable.MONTH_CURRENT =Integer.parseInt(date.split("/")[1]);
			GlobalVariable.YEAR_CURRENT =Integer.parseInt(date.split("/")[2]);
		}
		else
		{
			txtDate.setText(DateUtli.SpilitDate());
		}
		
		
		
		edittextPhoneNo.setOnClickListener(this);
		buttonSend.setOnClickListener(this);
		buttonStop.setOnClickListener(this);
		cbPickTime.setOnClickListener(this);
		cbCheckEveryday.setOnClickListener(this);
		
		linearsetTime.setOnClickListener(this);
		linearsetDate.setOnClickListener(this);
		
		txtTimeLoop.setOnClickListener(this);
		txtDateLoop.setOnClickListener(this);
		
		linearTimeLoop.setOnClickListener(this);
		linearRepateDate.setOnClickListener(this);
		textSMS.setOnLongClickListener(this);
		textSMS.setOnClickListener(this);
		edittextPhoneNo.setOnLongClickListener(this);
		edittextPhoneNo.setOnClickListener(this);
		
		imgEmail.setOnClickListener(this);
		
		btnSendNow.setOnClickListener(this);
		btnCancel.setOnClickListener(this);
		
		
		
		
		
		listviewPhone.setOnItemClickListener(new OnItemClickListener()
    	{
    		@Override
    		public void onItemClick(AdapterView<?> parent, View view, int position, long duration)
    		{
    			
    			
   				text += GlobalVariable.listContact.get(position).GetPhone()+",";
   				GlobalVariable.numberHidden+=GlobalVariable.listContact.get(position).GetPhone()+",";
   				Log.e("ListViewClick", "text = "+text);
    			edittextPhoneNo.setText(text);
    			
    			
    		}
    	});

	}
	
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		
		super.onPause();
		GlobalVariable.CHECK_START = false;
	}
	
	List<String> day = new ArrayList<String>();
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		//if(!GlobalVariable.CHECK_START)
		{
		
			if(GlobalVariable.RANDOM)
			{
				textSMS.setText("");
			}
			GlobalVariable.CHECK_START = true;
			if(!GlobalVariable.HIDDEN)
			{
				mScrollView.setVisibility(View.GONE);
	//			linearHiddenPickTime.setVisibility(View.GONE);
	//			linearHiddenRepeate.setVisibility(View.GONE);
	//			linearButtonPickTime.setVisibility(View.GONE);
				linearButtonSendSMS.setVisibility(View.VISIBLE);
	//			textSMS.setVisibility(View.GONE);
				
			}
			else
			{
				mScrollView.setVisibility(View.VISIBLE);
	//			linearHiddenPickTime.setVisibility(View.VISIBLE);
	//			linearHiddenRepeate.setVisibility(View.VISIBLE);
	//			linearButtonPickTime.setVisibility(View.VISIBLE);
				linearButtonSendSMS.setVisibility(View.GONE);
				extra 			= getIntent().getExtras();
				if(extra!=null)
				{
					String msg1 	= extra.getString("message");
					Log.e("SendSMSActivity", "msg = "+msg1);
					textSMS.setText(msg1);
				}
				
			}
			if(GlobalVariable.STARTTIME)
			{
				if(GlobalVariable.HOURS<10 && GlobalVariable.MINUTE<10)
				{
					txtTime.setText("0"+String.valueOf(GlobalVariable.HOURS) + ":" +"0"+String.valueOf(GlobalVariable.MINUTE));
					Log.w("onResume", "1");
					GlobalVariable.SetDefaultData(mContext, GlobalVariable.time, "0"+String.valueOf(GlobalVariable.HOURS) + ":" +"0"+String.valueOf(GlobalVariable.MINUTE));
				}
				else
				{
					if(GlobalVariable.MINUTE<10)
					{
						txtTime.setText(String.valueOf(GlobalVariable.HOURS) + ":" +"0"+String.valueOf(GlobalVariable.MINUTE));
						Log.w("onResume", "2");
						GlobalVariable.SetDefaultData(mContext, GlobalVariable.time, String.valueOf(GlobalVariable.HOURS) + ":" +"0"+String.valueOf(GlobalVariable.MINUTE));
					}
					else
					{
						if(GlobalVariable.HOURS<10)
						{
							txtTime.setText("0"+String.valueOf(GlobalVariable.HOURS) + ":" +String.valueOf(GlobalVariable.MINUTE));
							Log.w("onResume", "3");
							GlobalVariable.SetDefaultData(mContext, GlobalVariable.time, "0"+String.valueOf(GlobalVariable.HOURS) + ":" +String.valueOf(GlobalVariable.MINUTE));
						}
						else
						{
							txtTime.setText(String.valueOf(GlobalVariable.HOURS) + ":" +String.valueOf(GlobalVariable.MINUTE));
							GlobalVariable.SetDefaultData(mContext, GlobalVariable.time, String.valueOf(GlobalVariable.HOURS) + ":" +String.valueOf(GlobalVariable.MINUTE));
						}
					}
				}
				
				
				
			}
			
			if(GlobalVariable.STARTTIMELOOP)
			{
				if(GlobalVariable.HOURS_LOOP<10 && GlobalVariable.MINUTE_LOOP<10)
				{
					txtTimeLoop.setText("0"+String.valueOf(GlobalVariable.HOURS_LOOP) + ":" +"0"+String.valueOf(GlobalVariable.MINUTE_LOOP));
					GlobalVariable.SetDefaultData(mContext, GlobalVariable.timeloop, "0"+String.valueOf(GlobalVariable.HOURS_LOOP) + ":" +"0"+String.valueOf(GlobalVariable.MINUTE_LOOP));
					Log.w("onResumeLoop", "1");
				}
				else
				{
					if(GlobalVariable.MINUTE_LOOP<10)
					{
						txtTimeLoop.setText(String.valueOf(GlobalVariable.HOURS_LOOP) + ":" +"0"+String.valueOf(GlobalVariable.MINUTE_LOOP));
						GlobalVariable.SetDefaultData(mContext, GlobalVariable.timeloop, String.valueOf(GlobalVariable.HOURS_LOOP) + ":" +"0"+String.valueOf(GlobalVariable.MINUTE_LOOP));
						Log.w("onResumeLoop", "2");
					}
					else
					{
						if(GlobalVariable.HOURS_LOOP<10)
						{
							txtTimeLoop.setText("0"+String.valueOf(GlobalVariable.HOURS_LOOP) + ":" +String.valueOf(GlobalVariable.MINUTE_LOOP));
							GlobalVariable.SetDefaultData(mContext, GlobalVariable.timeloop, "0"+String.valueOf(GlobalVariable.HOURS_LOOP) + ":" +String.valueOf(GlobalVariable.MINUTE_LOOP));
							Log.w("onResumeLoop", "3");
						}
						else
						{
							txtTimeLoop.setText(String.valueOf(GlobalVariable.HOURS_LOOP) + ":" +String.valueOf(GlobalVariable.MINUTE_LOOP));
							GlobalVariable.SetDefaultData(mContext, GlobalVariable.timeloop, String.valueOf(GlobalVariable.HOURS_LOOP) + ":" +String.valueOf(GlobalVariable.MINUTE_LOOP));
						}
					}
				}
			}
			
			if(GlobalVariable.STARTDATE)
			{
				if(GlobalVariable.DATE<10 && GlobalVariable.MONTH<10)
				{
					txtDate.setText("0"+String.valueOf(GlobalVariable.DATE) + "/" +"0"+String.valueOf(GlobalVariable.MONTH)+"/"+String.valueOf(GlobalVariable.YEAR));
					Log.w("onResumeDate", "1");
				}
				else
				{
					if(GlobalVariable.MONTH<10)
					{
						txtDate.setText(String.valueOf(GlobalVariable.DATE) + "/" +"0"+String.valueOf(GlobalVariable.MONTH)+"/"+String.valueOf(GlobalVariable.YEAR));
						Log.w("onResumeDate", "2");
						GlobalVariable.SetDefaultData(mContext, GlobalVariable.date, String.valueOf(GlobalVariable.DATE) + "/" +"0"+String.valueOf(GlobalVariable.MONTH)+"/"+String.valueOf(GlobalVariable.YEAR));
					}
					else
					{
						if(GlobalVariable.DATE<10)
						{
							txtDate.setText("0"+String.valueOf(GlobalVariable.DATE) + "/" +String.valueOf(GlobalVariable.MONTH)+"/"+String.valueOf(GlobalVariable.YEAR));
							Log.w("onResumeDate", "3");
							GlobalVariable.SetDefaultData(mContext, GlobalVariable.date, "0"+String.valueOf(GlobalVariable.DATE) + "/" +String.valueOf(GlobalVariable.MONTH)+"/"+String.valueOf(GlobalVariable.YEAR));
						}
						else
						{
							txtDate.setText(String.valueOf(GlobalVariable.DATE) + "/" +String.valueOf(GlobalVariable.MONTH)+"/"+String.valueOf(GlobalVariable.YEAR));
							GlobalVariable.SetDefaultData(mContext, GlobalVariable.date, String.valueOf(GlobalVariable.DATE) + "/" +String.valueOf(GlobalVariable.MONTH)+"/"+String.valueOf(GlobalVariable.YEAR));
							Log.w("onResumeDate", "4");
						}
					}
				}
				
				
				
			}
			if(GlobalVariable.START_DATE_REPEAT)
			{
				/*Collections.sort(GlobalVariable.arrID, new Comparator<String>() {
		    		  
	
					@Override
					public int compare(String o1, String o2) {
						// TODO Auto-generated method stub
						return o1.compareTo(o2);
					}
		    		});
				
				
				String string = "";
				Log.w("aaaaaaaaaaaaaa", "GlobalVariable.arrID = "+GlobalVariable.arrID);
				for(int i = 0; i<GlobalVariable.arrID.size(); i++)
				{
					if(i<GlobalVariable.arrID.size()-1)
					{
						string +=GlobalVariable.arrID.get(i).substring(1,4)+",";
					}
					else
					{
						if(i ==GlobalVariable.arrID.size()-1)
							string +=GlobalVariable.arrID.get(i).substring(1,4);
					}
				}
				GlobalVariable.ARRAY_ID = string;
				Log.w("aaaaaaaaaaaaaa", "GlobalVariable.ARRAY_ID = "+GlobalVariable.ARRAY_ID);
				txtDateLoop.setText(string);
				GlobalVariable.SetDefaultData(mContext, GlobalVariable.dateRepeat, GlobalVariable.ARRAY_ID);*/
				txtDateLoop.setText(GlobalVariable.ARRAY_ID);
			}
			
		}
		
		
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		
		inflater.inflate(R.layout.menu, menu);
		
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle item selection
		switch (item.getItemId()) {
		case R.id.huongdan:
			Intent intent1 = new Intent(mContext, HelpAppActivity.class);
        	mContext.startActivity(intent1);
            break;
		case R.id.gopy:
			Intent intent = new Intent(mContext, AboutAppActivity.class);
        	mContext.startActivity(intent);
            break;
		case R.id.thoat:
			finish();
			break;
	
		default:
			return super.onOptionsItemSelected(item);
		}
		return true;
	}
	
	private PendingIntent mAlarmSender;
	private AlarmManager mAlarmManager;
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.buttonStart:
			
//			if(edittextPhoneNo.length()==0)
//			{
//				Toast.makeText(getApplicationContext(),
//						"Please input number phone",
//						Toast.LENGTH_LONG).show();
//			}
//			else
			{
				if(cbCheckEveryday.isChecked() || cbPickTime.isChecked())
				{
					
					stopService(new Intent(this, AlarmService.class));
					Log.d("SendSMSActivity", "onClick: starting service");
					//startService(new Intent(this, AlarmService.class));
					long firstAlarmTime = SystemClock.elapsedRealtime();

		            // Sets a repeating countdown timer that triggers AlarmService
		            mAlarmManager.setRepeating(
		                AlarmManager.ELAPSED_REALTIME_WAKEUP, // based on time since last wake up
		                firstAlarmTime,  // sends the first alarm immediately
		                THIRTY_SECONDS_MILLIS,  // repeats every thirty seconds
		                mAlarmSender  // when the alarm goes off, sends this Intent
		            );
					
					
					
					GlobalVariable.SetDefaultData(mContext, GlobalVariable.SETTINGS_SERVICE_STATUS,String.valueOf(isMyServiceRunning()));
					Log.e("status", "trang thai = "+isMyServiceRunning());
					finish();
					
				}
				else
				{
					Toast.makeText(getApplicationContext(),
							"Please check to checkbox !",
							Toast.LENGTH_LONG).show();	
				}
			}
			
			break;
		case R.id.btnStop:
			Log.d("SendSMSActivity", "onClick: stopping srvice");
		    stopService(new Intent(this, AlarmService.class));
		    finish();
			break;
		case R.id.cbPickTime:
			if(edittextPhoneNo.length()!=0 && textSMS.length()!=0)
			{
			
					if(cbPickTime.isChecked())
					{
						
						if(edittextPhoneNo.length()==0)
						{
							Toast.makeText(getApplicationContext(),
									"Please input number phone",
									Toast.LENGTH_LONG).show();
						}
						else
						{
							String t = edittextPhoneNo.getText().toString().substring(0, edittextPhoneNo.getText().toString().length());
							Log.w("Onclick", "t = "+t);
							GlobalVariable.SetDefaultData(mContext, GlobalVariable.number, t);
							GlobalVariable.SetDefaultData(mContext, GlobalVariable.message, textSMS.getText().toString());
							GlobalVariable.SetDefaultData(mContext, GlobalVariable.picktime, "true");
						}
						GlobalVariable.SetDefaultData(mContext, GlobalVariable.picktime, "true");
						edittextPhoneNo.setText("");
						textSMS.setText("");
						Toast.makeText(getApplicationContext(),
								"Message and number phone saved !",
								Toast.LENGTH_LONG).show();	
					}
					else
					{
						GlobalVariable.SetDefaultData(mContext, GlobalVariable.picktime, "false");
					}
					
					
			}
			else
			{
				Toast.makeText(getApplicationContext(),
						"Input message and phone !",
						Toast.LENGTH_LONG).show();	
			}
			break;
			
		case R.id.cbcheckEveryday:
			
			if(edittextPhoneNo.length()!=0 && textSMS.length()!=0)
			{
				if(cbCheckEveryday.isChecked())
				{
					if(textSMS.length()!=0)
					{
						Log.e("onlick","check every date");
						
						String t = edittextPhoneNo.getText().toString().substring(0, edittextPhoneNo.getText().toString().length());
						Log.w("Onclick", "t = "+t);
						Log.w("SendSMS", "t2.size = "+t.split(",").length);
						GlobalVariable.SetDefaultData(mContext, GlobalVariable.messageRepeat, textSMS.getText().toString());
						
						GlobalVariable.SetDefaultData(mContext, GlobalVariable.numberRepeat, t);
					}
					else
					{
						if(edittextPhoneNo.length()==0)
						{
							Toast.makeText(getApplicationContext(),
									"Please input number phone",
									Toast.LENGTH_LONG).show();
						}
						else
						{
							if(textSMS.length()==0)
							{
								String t1 = edittextPhoneNo.getText().toString().substring(0, edittextPhoneNo.getText().toString().length()-1);
								GlobalVariable.SetDefaultData(mContext, GlobalVariable.numberRepeat, t1);
								extra1 			= getIntent().getExtras();
								if(extra1!=null)
								{
									String msg1 	= extra1.getString("string");
									Log.e("SendSMSActivity", "string = "+msg1);
									String t2 = msg1.substring(0,msg1.length()-1);
									Log.w("Onclick", "t2 = "+t2);
									Log.w("SendSMS", "t2.size = "+t2.split("/").length);
									GlobalVariable.SetDefaultData(mContext, GlobalVariable.random, t2);
								}
							}
						}
					}
					GlobalVariable.SetDefaultData(mContext, GlobalVariable.checkeverydate, "true");
				}
				else
				{
					GlobalVariable.SetDefaultData(mContext, GlobalVariable.checkeverydate, "false");
				}
			}
			else
			{
				Toast.makeText(getApplicationContext(),
						"Input message and phone !",
						Toast.LENGTH_LONG).show();	
			}
			
			break;
		case R.id.linearSetTime:
			Intent myIntent = new Intent(mContext, SelectTimeActivity.class);
			startActivity(myIntent);
			break;
			
		case R.id.linearSetDate:
			Intent myIntent1 = new Intent(mContext, SelectDateActivity.class);
			startActivity(myIntent1);
			break;
			
		case R.id.linearTimeLoop:
			Intent myIntent2 = new Intent(mContext, SelectTimeLoopActivity.class);
			startActivity(myIntent2);
			break;
		case R.id.linearRepateDate:
			Intent myIntent3 = new Intent(mContext, RepeatDateActivity.class);
			startActivity(myIntent3);
			break;
		case R.id.txtDateLoop:
			Intent myIntent4 = new Intent(mContext, RepeatDateActivity.class);
			startActivity(myIntent4);
			break;
		case R.id.email:
			Intent myIntent5 = new Intent(mContext, MessageActivity.class);
			startActivity(myIntent5);
			break;
			
		case R.id.buttonSendNow:
			extra 			= getIntent().getExtras();
			String msg 	= extra.getString("message");
			if(edittextPhoneNo.getText().toString().split(",").length>0)
			{
				Log.w("lengh", "number.length = "+edittextPhoneNo.getText().toString().split(",").length);
				for(int i = 0; i< edittextPhoneNo.getText().toString().split(",").length; i++)
				{
//		    		SmsManager smsManager = SmsManager.getDefault();
					Log.e("hung", "number = "+edittextPhoneNo.getText().toString().split(",")[i]);
					Log.e("hung", "message = "+msg);
//		    		smsManager.sendTextMessage(edittextPhoneNo.getText().toString().split(",")[i], null, msg, null, null);
					Log.e("handle", "da send sms");
					Toast.makeText(getApplicationContext(), "SMS Sent!",
							Toast.LENGTH_LONG).show();
				}
			}
			break;
		case R.id.btnCancelNow:
			GlobalVariable.HIDDEN = true;
			startActivity(getIntent()); 
			finish();

			break;
		case R.id.editTextPhoneNo:
			
			Toast.makeText(getApplicationContext(),
					"Press long click to delete number !",
					Toast.LENGTH_LONG).show();	
			if(imgUp.getVisibility() == View.VISIBLE)
			{
				listviewPhone.setVisibility(View.VISIBLE);
				imgUp.setVisibility(View.GONE);
				imgDown.setVisibility(View.VISIBLE);
			}
			else
			{
				listviewPhone.setVisibility(View.GONE);
				imgUp.setVisibility(View.VISIBLE);
				imgDown.setVisibility(View.GONE);
			}
			break;
		case R.id.editTextSMS:
			Toast.makeText(getApplicationContext(),
					"Press long click to enable !",
					Toast.LENGTH_LONG).show();
			break;

		default:
			break;
		}
		
	}
	
	//kiem tra de khoi dong lai sau khi boot
	private boolean isMyServiceRunning()
    {
        ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
        for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE))
        {
            if ("org.me.SendSMS.AlarmService".equals(service.service.getClassName()))
            {
                return true;
            }
        }
        return false;
    }


	@Override
	public boolean onLongClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.editTextPhoneNo:
			text="";
			edittextPhoneNo.setText("");
			break;
		case R.id.editTextSMS:
			textSMS.setEnabled(true);
			
			break;

		default:
			break;
		}
		
		return false;
	}
}